//
//  DFUtil.swift
//  DailyFresh
//
//  Created by Arthur on 2017/9/26.
//  Copyright © 2017年 DailyFresh. All rights reserved.
//

import Foundation

// 判断数组是否为空
func isArrayEmpty(_ array:Array<Any>?) -> Bool {
    return array?.isEmpty == true
}

func isStrEmpty(_ string: String?) -> Bool {
    return (string ?? "").isEmpty
}

/** 本地版本号*/
func _getLocalVersion() -> String {
    let localversion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
    return localversion
}

/** 比较版本号*/
func _compareVersion(_ localVersion:String, _ serverVersion:String) -> Bool {
    
    if isStrEmpty(localVersion) || isStrEmpty(serverVersion){
        return false
    }
    
    let local:Array<String> = localVersion.characters.split{$0 == "."}.map(String.init)
    let server:Array<String> = serverVersion.characters.split{$0 == "."}.map(String.init)
    
    let minCount = local.count > server.count ? server.count : local.count
    
    for i in 0..<minCount
    {
        if((local[i]  as NSString).intValue < (server[i]  as NSString).intValue){
            return true
        }else if((local[i]  as NSString).intValue > (server[i]  as NSString).intValue){
            return false
        }
    }
    
    if server.count > local.count {
        return true
    }else if server.count <= local.count {
        return false
    }
    
    return false
}

// 同步
func synchronized(lock: AnyObject, closure: () -> ()) {
    objc_sync_enter(lock)
    closure()
    objc_sync_exit(lock)
}

